package com.auto.code.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.HtmlUtils;

import com.auto.code.domain.ResultInfo;
import com.auto.code.domain.TemplateDO;
import com.auto.code.domain.TemplateDTO;
import com.auto.code.domain.TemplateGroupDTO;
import com.auto.code.domain.TemplatePathDTO;
import com.auto.code.service.TemplateGroupService;
import com.auto.code.service.TemplatePathService;
import com.auto.code.service.TemplateService;
import com.github.pagehelper.PageInfo;

/**
 * 模板控制层
 */
@Controller
@RequestMapping("/auto/code/template")
public class TemplateController {
	@Autowired
    private TemplateService templateService;

    @Autowired
    private TemplateGroupService templateGroupService;

    @Autowired
    private TemplatePathService templatePathService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(Model model) {
	    //查询所有的模板分类
        List<TemplateGroupDTO> dtoList = templateGroupService.queryList(new TemplateGroupDTO());
        model.addAttribute("groupDtoList", dtoList);
        return new ModelAndView("/page/template/template_list");
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ModelAndView tableList(Model model, TemplateDTO templateDTO) {
        PageInfo<TemplateDTO> pageList = templateService.queryPage(templateDTO);
        model.addAttribute("pageInfo", pageList);
        return new ModelAndView("/page/template/template_table_list");
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView createPage(Model model, TemplateDTO templateDTO) {
    	model.addAttribute("groupId", templateDTO.getGroupId());
    	List<TemplatePathDTO> pathList = templatePathService.queryList(new TemplatePathDTO());
    	model.addAttribute("pathList", pathList);
        return new ModelAndView("/page/template/template_create");
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo createSave(Model model, TemplateDTO templateDTO) {
        templateService.create(templateDTO);
        return  ResultInfo.ok();
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo del(@RequestParam(required = false, value = "idList[]") List<Long> idList) {
	    for (Long id : idList){
            templateService.deleteById(id);
        }
        return ResultInfo.ok();
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ModelAndView detail(Model model, Long id) {
        model.addAttribute("template", templateService.getById(id));
        return new ModelAndView("/page/template/template_detail");
    }

    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public ModelAndView modifyPage(Model model, Long id) {
    	TemplateDO dto = templateService.getById(id);
    	dto.setContent(HtmlUtils.htmlEscape(dto.getContent()));
        model.addAttribute("template", dto);
        return new ModelAndView("/page/template/template_modify");
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo modifySave(Model model, TemplateDTO templateDTO) {
        templateService.update(templateDTO);
        return  ResultInfo.ok();
    }
}
